package table

import (
	"fmt"
	"i18n-template/components"

	"gorm.io/gorm"
)

type File struct {
	gorm.Model
	Language string `json:"language" gorm:"type:string;size:20;comment:语言"`
	UUID     string `json:"uuid" gorm:"type:string;size:256;comment:唯一标识"`
	Filename string `json:"filename" gorm:"type:string;size:256;comment:文件的名字"`
	Filetype string `json:"filetype" gorm:"type:string;size:20;comment:文件的类型->图片|文件"`
	Filesize int64  `json:"filesize" gorm:"type:uint;size:256;comment:文件的大小"`
	URL      string `json:"url" gorm:"comment:URL"`
}

func (f File) TableName() string {
	return "file"
}

func (f File) GenerateFileService(F File) error {
	return components.DB.Model(&File{}).Save(&F).Error
}
func (f File) FindFileByUUID(uuid string) (*File, error) {
	file := &File{}
	err := components.DB.Model(&File{}).Where("uuid = ?", uuid).First(&file).Error
	return file, err
}

func (f File) FindFileByTypeAndLng(F File) []File {
	var Files []File
	sqlStr := fmt.Sprintf("language = '%s'", f.Language)
	components.DB.Debug().Model(&File{}).
		Where(sqlStr).
		Find(&Files)
	return Files
}
